package main

import (
	"fmt"
	"sort"
	"strconv"
)

// multiple accesses
// 给 list of [name, time], time is string format: '1300' //下午一点
// return: list of names and the times where their swipe badges within one hour. if there are multiple intervals that satisfy the condition, return any one of them.
// name1: time1, time2, time3...
// name2: time1, time2, time3, time4, time5...
// example:
// input: [['James', '1300'], ['Martha', '1600'], ['Martha', '1620'], ['Martha', '1530']]
// output: {
// 'Martha': ['1600', '1620', '1530']
// }
// badge_times = [
// ["Paul", "1355"],
// ["Jennifer", "1910"],
// ["John", "835"],
// ["John", "830"],
// ["Paul", "1315"],
// ["John", "1615"],
// ["John", "1640"],
// ["Paul", "1405"],
// ["John", "855"],
// ["John", "930"],
// ["John", "915"],
// ["John", "730"],
// ["John", "940"],
// ["Jennifer", "1335"],
// ["Jennifer", "730"],
// ["John", "1630"],
// ["Jennifer", "5"]
// ]

// Expected output (in any order)
// John: 830 835 855 915 930
// Paul: 1315 1355 1405

// badge_records = [
//   ["Martha",   "exit"],
//   ["Paul",     "enter"],
//   ["Martha",   "enter"],
//   ["Martha",   "exit"],
//   ["Jennifer", "enter"],
//   ["Paul",     "enter"],
//   ["Curtis",   "enter"],
//   ["Paul",     "exit"],
//   ["Martha",   "enter"],
//   ["Martha",   "exit"],
//   ["Jennifer", "exit"],
// ]

// Expected output: ["Paul", "Curtis"], ["Martha"]

func findBadgeIssues(badgeRecords [][2]string) {
	m := make(map[string][]string)

	for _, record := range badgeRecords {
		if _, ok := m[record[0]]; ok == false {
			m[record[0]] = []string{record[1]}
		} else {
			m[record[0]] = append(m[record[0]], record[1])
		}
	}

	for name, records := range m {
		cnt := 0
		for _, record := range records {
			if record == "enter" {
				cnt++
			} else {
				cnt--
			}
		}
		if cnt > 0 {
			fmt.Printf("%s exited without badge.\n", name)
		} else if cnt < 0 {
			fmt.Printf("%s entered without badge.\n", name)

		}
	}
	// fmt.Println(m)
}

func invalidAccesses(input [][2]string) {
	m := make(map[string][]int)

	for _, record := range input {
		time, _ := strconv.Atoi(record[1])
		if _, ok := m[record[0]]; ok == false {
			m[record[0]] = []int{time}
		} else {
			m[record[0]] = append(m[record[0]], time)
		}
	}

	for name, records := range m {
		sort.Ints(records)
		startTime := records[0]
		for i := 1; i < len(records); i++ {
			if records[i]-startTime <= 100 {
				fmt.Println(name)
				fmt.Println(startTime)
				fmt.Println(records[i])
				break
			} else {
				startTime = records[i]
			}
		}
	}

	fmt.Println(m)
}

func main() {
	input := [][2]string{
		[2]string{"Martha", "exit"},
		[2]string{"Paul", "enter"},
		[2]string{"Martha", "enter"},
		[2]string{"Martha", "exit"},
		[2]string{"Jennifer", "enter"},
		[2]string{"Paul", "enter"},
		[2]string{"Curtis", "enter"},
		[2]string{"Paul", "exit"},
		[2]string{"Martha", "enter"},
		[2]string{"Martha", "exit"},
		[2]string{"Jennifer", "exit"},
	}
	input2 := [][2]string{
		[2]string{"Martha", "1600"},
		[2]string{"Martha", "1620"},
		[2]string{"Martha", "1530"},
		[2]string{"James", "1300"},
		[2]string{"James", "1330"},
	}
	findBadgeIssues(input)
	invalidAccesses(input2)
}
